from django.shortcuts import render
from rest_framework import  views
from rest_framework import  viewsets
from rest_framework.authtoken.serializers import AuthTokenSerializer
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from django.contrib.auth import get_user_model
from django.utils.timezone import now
from rest_framework import status
#不同随机的uuid的信息
import shortuuid
import  os
from django.conf import settings


# Create your views here.
from .authorizations import generate_jwt,JWTAuthentication
from .serializers import UserSerializer
from .models import StormUser

class LoginView(views.APIView):
	def get(self,request):
		return Response({'status':0,'msg':'ok'})

	def post(self,request):
		serializer=AuthTokenSerializer(data=request.data,context={'request':request})
		if serializer.is_valid():
			user=serializer.validated_data.get('user')
			print('用户信息:\n',user)
			print(StormUser.object.filter(telephone__contains=user))
			#用户最后一次登录的时间
			user.last_login=now()
			user.save()
			#验证以及生成token的信息
			token=generate_jwt(user=user)
			user_serializer=UserSerializer(user)
			return Response(
				data={"token":token,"user":user_serializer.data},
				status=status.HTTP_200_OK)
		else:
			print(serializer.errors)
			return Response(
				data={'status':10001,'msg':'登录的用户名或者密码无效,请检查！'},
				status=status.HTTP_500_INTERNAL_SERVER_ERROR)

class SettingsView(views.APIView):
	'''获取用户的基本信息'''
	authentication_classes = [JWTAuthentication]
	permission_classes = [IsAuthenticated]

	def get(self,request):
		#获取用户下所有的模型信息
		users=StormUser.object.all()
		serializer=UserSerializer(users,many=True)
		return Response(data=serializer.data,status=status.HTTP_200_OK)

	def put(self,request):
		'''实现设置提交后的信息'''
		user=request.user
		user.username=request.data.get('username')
		user.telephone=request.data.get('telephone')
		user.email=request.data.get('email')
		user.avator=request.data.get('avator')
		user.save()
		serializer=UserSerializer(user)
		return Response(data=serializer.data,status=status.HTTP_200_OK)

class DisableUser(views.APIView):
	def put(self,request,uid):
		user=StormUser.object.get(uid=uid)
		user.username=request.data.get('username')
		user.telephone=request.data.get('telephone')
		user.email=request.data.get('email')
		user.avator=request.data.get('avator')
		user.is_active=request.data.get('is_active')
		user.is_active=False
		user.save()
		serializer=UserSerializer(user)
		return Response(data=serializer.data,status=status.HTTP_200_OK)

class EnableUser(views.APIView):
	'''启用用户'''
	def put(self,request,uid):
		user=StormUser.object.get(uid=uid)
		user.username=request.data.get('username')
		user.telephone=request.data.get('telephone')
		user.email=request.data.get('email')
		user.avator=request.data.get('avator')
		user.is_active=request.data.get('is_active')
		user.is_active=True
		user.save()
		serializer=UserSerializer(user)
		return Response(data=serializer.data,status=status.HTTP_200_OK)

class AvatorUploadView(views.APIView):
	'''文件上传的处理'''
	authentication_classes = [JWTAuthentication]
	permission_classes = [IsAuthenticated]

	def save_file(self,file):
		'''
		保存上传的文件信息
		:param file: 要操作的文件信息
		:return:
		'''
		#获取文件名称,如login.jpg的就会处理为login和jpg
		filename=shortuuid.uuid()+os.path.splitext(file.name)[-1]
		print('filename的信息:\n',filename)
		with open(os.path.join(settings.MEDIA_ROOT,filename),'wb') as f:
			for chunk in file.chunks():
				f.write(chunk)
		#把上传的文件保存到MEDIA_URL的路径中
		print('文件路径:\n',settings.MEDIA_URL)
		return settings.MEDIA_URL+filename


	def post(self,request):
		file=request.FILES.get('file')
		filePath=self.save_file(file=file)
		#构建完整的绝对的文件路径
		fileUrl=request.build_absolute_uri(filePath)
		print('文件存储的完整路径:'+fileUrl)
		return Response(data={'picture':fileUrl},status=status.HTTP_200_OK)


